/*
 * CommunicationRobot11.cpp
 *
 *  Created on: 15/11/2013
 *      Author: gonzalo
 */

#include "CommunicationRobot11.h"

namespace middleware {

const std::string CommunicationRobot11::ROBOT11_SEM_PATH = "/tmp/robot11";
const std::string CommunicationRobot11::ROBOT11_SHMEM_PATH = "/tmp/robot11mem";

CommunicationRobot11::CommunicationRobot11(char key) : CommunicationRobotPiezas(key) {
	this->key = key;
	this->semRobot = ipc::Semaphore::get(ROBOT11_SEM_PATH, key);
	this->shMemEstado = ipc::SharedMemoryInterface<EstadoRobotPiezas>::get(ROBOT11_SHMEM_PATH, key);
	this->semEsperaRobot12 = ipc::Semaphore::get(ROBOT11_SEM_PATH, CommunicationRobotPiezas::getAltKey(key));
}

void CommunicationRobot11::create(char key) {
	ipc::Semaphore::create(ROBOT11_SEM_PATH, key).initialize(0);
	ipc::Semaphore::create(ROBOT11_SEM_PATH, CommunicationRobotPiezas::getAltKey(key)).initialize(0);
	ipc::SharedMemoryInterface<EstadoRobotPiezas> tmpMem = ipc::SharedMemoryInterface<EstadoRobotPiezas>::create(ROBOT11_SHMEM_PATH, key);
	tmpMem.lock();
	tmpMem.write(ROBOT_INACTIVO);
	tmpMem.unlock();
}

void CommunicationRobot11::destroy(char key) {
	ipc::Semaphore::destroy(ROBOT11_SEM_PATH, key);
	ipc::Semaphore::destroy(ROBOT11_SEM_PATH, CommunicationRobotPiezas::getAltKey(key));
	ipc::SharedMemoryInterface<EstadoRobotPiezas>::destroy(ROBOT11_SHMEM_PATH, key);
}

void CommunicationRobot11::esperarRobot12() {
	this->semEsperaRobot12.wait();
}

void CommunicationRobot11::robot12EstaListo() {
	this->semEsperaRobot12.signal();
}

std::string CommunicationRobot11::getNombre() {
	return "robot 11";
}

CommunicationRobot11::~CommunicationRobot11() {
}

}
/* namespace middleware */
